package com.yserp.project.erp.basic.goodstype.controller;

import com.yserp.common.utils.StringUtils;
import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.common.utils.security.ShiroUtils;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.domain.Ztree;
import com.yserp.project.erp.basic.goods.domain.BasisGoods;
import com.yserp.project.erp.basic.goods.service.IBasisGoodsService;
import com.yserp.project.erp.basic.goodstype.domain.BasisGoodsType;
import com.yserp.project.erp.basic.goodstype.service.IBasisGoodsTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品类别Controller
 *
 * @author yserp
 * @date 2022-06-29
 */
@Controller
@RequestMapping("/basic/goodstype")
public class BasisGoodsTypeController extends BaseController {
    private String prefix = "erp/basic/goodstype";

    @Autowired
    private IBasisGoodsTypeService basisGoodsTypeService;
    @Autowired
    private IBasisGoodsService basisGoodsService;

    @RequiresPermissions("basic:goodstype:view")
    @GetMapping()
    public String goodstype() {
        return prefix + "/goodsType";
    }

    /**
     * 查询商品类别列表
     */
    @RequiresPermissions("basic:goodstype:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BasisGoodsType> list(BasisGoodsType basisGoodsType) {
        List<BasisGoodsType> list = basisGoodsTypeService.selectBasisGoodsTypeList(basisGoodsType);
        return list;
    }

    /**
     * 导出商品类别列表
     */
    @RequiresPermissions("basic:goodstype:export")
    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasisGoodsType basisGoodsType) {
        List<BasisGoodsType> list = basisGoodsTypeService.selectBasisGoodsTypeList(basisGoodsType);
        ExcelUtil<BasisGoodsType> util = new ExcelUtil<BasisGoodsType>(BasisGoodsType.class);
        return util.exportExcel(list, "商品类别数据");
    }

    /**
     * 新增商品类别
     */
    @GetMapping(value = {"/add/{id}", "/add/"})
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("goodsType", basisGoodsTypeService.selectBasisGoodsTypeById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存商品类别
     */
    @RequiresPermissions("basic:goodstype:add")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasisGoodsType basisGoodsType) {
        basisGoodsType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basisGoodsTypeService.insertBasisGoodsType(basisGoodsType));
    }

    /**
     * 修改商品类别
     */
    @RequiresPermissions("basic:goodstype:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BasisGoodsType basisGoodsType = basisGoodsTypeService.selectBasisGoodsTypeById(id);
        BasisGoodsType parentGoodsType = basisGoodsTypeService.selectBasisGoodsTypeById(basisGoodsType.getParentId());
        mmap.put("goodsType", basisGoodsType);
        mmap.put("parentGoodsType", parentGoodsType);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品类别
     */
    @RequiresPermissions("basic:goodstype:edit")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasisGoodsType basisGoodsType) {
        if (basisGoodsType.getParentId().equals(basisGoodsType.getId())) {
            return error("修改商品类别'" + basisGoodsType.getGoodsTypeName() + "'失败，上级商品类别不能是自己");
        }
        basisGoodsType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basisGoodsTypeService.updateBasisGoodsType(basisGoodsType));
    }

    /**
     * 删除商品类别
     */
    @RequiresPermissions("basic:goodstype:remove")
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        if (basisGoodsTypeService.selectGoodsTypeCount(id) > 0) {
            return AjaxResult.warn("存在下级商品类别,不允许删除");
        }
        BasisGoods goods = new BasisGoods();
        goods.setGoodsTypeId(id);
        if(basisGoodsService.selectGoodsCount(goods) > 0){
            return AjaxResult.warn("存在商品,不允许删除");
        }
        return toAjax(basisGoodsTypeService.deleteBasisGoodsTypeById(id));
    }

    /**
     * 选择商品类别树
     */
    @GetMapping(value = {"/selectGoodsTypeTree/{id}", "/selectGoodsTypeTree/"})
    public String selectGoodsTypeTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap) {
        if (StringUtils.isNotNull(id)) {
            mmap.put("goodsType", basisGoodsTypeService.selectBasisGoodsTypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载商品类别树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = basisGoodsTypeService.selectGoodsTypeTree();
        return ztrees;
    }

    /**
     * 校验商品类别编码
     */
    @PostMapping("/checkGoodsTypeCodeUnique")
    @ResponseBody
    public String checkGoodsTypeCodeUnique(BasisGoodsType basisGoodsType) {
        return basisGoodsTypeService.checkGoodsTypeCodeUnique(basisGoodsType.getGoodsTypeCode());
    }
}
